package core.scheduledtask;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Date;
import java.util.TimerTask;

import org.apache.log4j.Logger;

import core.IEC104Server;

import util.Timehelper;
import dbop.DBCPDatabaseAccess;
import dbop.DBManager;

public class getDBDataTask extends TimerTask {
	private static Logger logger = Logger.getLogger(getDBDataTask.class);
	public getDBDataTask() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			getYXdata();
			getYCdata();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void getYXdata() throws SQLException{
		Connection conn= DBManager.getmysqlConn("DB.MYSQL");
		if (conn!=null){
			Statement stmt = conn.createStatement();
			
			String sql= "SELECT * from vw_yxdata order by vw_yxdata.FINT_INFO  asc ";
			
			boolean result = stmt.execute(sql);
			if (result) {
				
				ResultSet res = stmt.getResultSet();
				int count=0;
				while (res.next()) {
					count++;
					String point            =res.getString(1);
					String value      =res.getString(2);
					
					IEC104Server.yxdata.put(point, value);
					
					
				}
				logger.info("yx: "+IEC104Server.yxdata.size());
				res.close();
			}
			stmt.close();
		}
		conn.close();
	}
	private void getYCdata() throws SQLException{
		Connection conn= DBManager.getmysqlConn("DB.MYSQL");
		if (conn!=null){
			Statement stmt = conn.createStatement();
			
			String sql= "SELECT * from vw_ycdata order by vw_ycdata.FINT_INFO  asc ";
			
			boolean result = stmt.execute(sql);
			if (result) {
				
				ResultSet res = stmt.getResultSet();
				int count=0;
				while (res.next()) {
					count++;
					String point            =res.getString(1);
					String value      =res.getString(2);
					
					IEC104Server.ycdata.put(point, value);
					
					
				}
				logger.info("yc: "+IEC104Server.ycdata.size());
				res.close();
			}
			stmt.close();
		}
		conn.close();
	}

}
